package com.tianpengtech.front.mobile.publisher;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.tianpengtech.admin.dictionary.DictionaryDao;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.task.TaskWh;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.intercepter.TokenCheckInterceptor;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.UserUtil;
import com.tianpengtech.front.web.common.controller.BaseController;

/**
 * Created by ggd on 2017/8/3 0003.
 */
public class MobilePublishController extends BaseController {

    private static MobilePublishService service = MobilePublishService.me;
    private DictionaryDao dictionaryDao = new DictionaryDao();
    /**
     * 获取商家信息
     */
    public void getPublish(){
        String id=getPara("id");
        if (StrKit.isBlank(id)){
            renderJson(R.fail("id不能为空"));
            return;
        }
        R r=service.getPublish(id);
        renderJson(r);
    }


    /**
     * 获取商家任务信息
     */
    public void getPublishTask(){
        String id=getPara("id");
        if (StrKit.isBlank(id)){
            renderJson(R.fail("id不能为空"));
            return;
        }
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",5);
        R r=service.getPublishTask(pageNum,pageSize,id);
        renderJson(r);
    }


    /**
     * 获取商家评论信息
     */
    public void getPublishComment(){
        String id=getPara("id");
        if (StrKit.isBlank(id)){
            renderJson(R.fail("id不能为空"));
            return;
        }
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",5);
        R r=service.getPublishComment(pageNum,pageSize,id);
        renderJson(r);
    }

    /**
     * 获取举报信息
     */
    public void getReportInfo(){
        R r = R.ok();
        r.p("reports",dictionaryDao.readParents(200100));
        renderJson(r);
    }

    /**
     * 保存举报信息
     */
    public void saveReport(){
        String content = getPara("content");
        String contentId = getPara("contentId");
        String userId = getPara("userId");
        User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        long uid=user.getLong(User.ID);
        if (StrKit.isBlank(content) && StrKit.isBlank(contentId)){
            renderJson(R.fail("请输入举报原因"));
            return;
        }
        if (StrKit.isBlank(userId)){
            renderJson(R.fail("请输入被举报人信息"));
            return;
        }
        R r=service.saveReport(contentId,content,uid,userId);
        renderJson(r);
    }

    
    @Clear(TokenCheckInterceptor.class)
    public void saveComment(){
    	
    	int taskId = getParaToInt("taskId",0);
    	User user= UserUtil.getCurrentUser(getPara("token"),getHeader("source"));
        long uid=user.getLong(User.ID);
    	String content = getPara("comment");
    	String sql = "select * from fair_task_wh where wh_id=? and task_id=? limit 1";
    	TaskWh wh = TaskWh.dao.findFirst(sql,uid,taskId);
    	if(wh!=null){
    		if(StringUtils.isEmpty(wh.getStr(TaskWh.EVALUATE_WHOM))){
    			wh.set(TaskWh.EVALUATE_WHOM, content).set(TaskWh.WHOM_DATE, new Date()).update();
    			renderJson(R.ok("评论成功"));
    			return ;
    		}else{
    			renderJson(R.fail("你已经对当前任务进行过评价了，感谢你的参与"));
    			return ;
    		}
    	}else{
    		renderJson(R.fail("你没有参加当前任务，不允许评价！！"));
    	}
    }

    public void getCommentByTask(){
    	int taskId = getParaToInt("id",0);
    	String sql="select * from fair_task_wh where task_id = ? order by id desc";
    	R r= new R(); 
    	List<Map<String,Object>> comments  = new ArrayList<Map<String,Object>>();
    	for (Record record : Db.find(sql,taskId)) {
			if(StringUtils.isNotEmpty(record.get(TaskWh.EVALUATE_WHOM))){
				Map<String,Object> result  = new HashMap<String,Object>();
	    		Fair wh = Fair.dao.findById(record.getInt(TaskWh.WH_ID));
	    		String avatar=(wh.getStr(Fair.PHOTO).equals("/upload/avatar/x.jpg"))?GlobalConfigConstant.defaultAvatar:wh.getStr(Fair.PHOTO);
	    		result.put("nickName",wh.getStr(Fair.NICK_NAME));
	    		result.put("pubdate",record.get(TaskWh.WHOM_DATE));
	    		result.put("content",record.get(TaskWh.EVALUATE_WHOM));
	    		result.put("avatar",avatar);
				comments.add(result);
			}
		}
    	r.p("code", 1);
    	r.p("comments",comments);
    	renderJson(r);
    }

}
